package stack;

public class BractetsMatch {
    public static void main(String[] args) {
        String str = "(上海(长安)())";
        boolean match = isMatch(str);
        System.out.println(str+"中的括号是否匹配："+match);
    }
    /**
     * 判断str中的括号是否匹配
     * @param str 括号组成的字符串
     * @return 如果匹配，返回true，如果不匹配，返回false
     */
    public static boolean isMatch(String str){
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++){
            String st = str.charAt(i) + "";
            if (st.equals("(")){
                stack.push(st);
            }
            if (st.equals(")")){
                String pop = stack.pop();
                if (pop == null){
                    return false;
                }
            }
        }
        if (stack.size() == 0){
            return true;
        }else {
            return false;
        }
    }

}
